 var Pwa_Install = function (VERSION) {
   var _cache_list = 'cache_list';
   var cache_list = [];

   var domain = '-{dm}-';
   for (var i = 0; i < _cache_list.length; i++) {
     var url = _cache_list[i];

     url = url.replace('{dm}', domain);

     cache_list.push(url);


   }

   var newArr = [];
   for (var i = 0; i < cache_list.length; i++) {
     if (newArr.indexOf(cache_list[i]) < 0) {
       newArr.push(cache_list[i]);
     }
   }
   cache_list = newArr;

   self.addEventListener('install', function (event) {
     //self.skipWaiting()
     event.waitUntil(
       caches.open(VERSION).then(function (cache) {
         return cache.addAll(cache_list).then(
           self.skipWaiting()
         )
       })
     );
   });

   self.addEventListener('fetch', function (event) {
     event.respondWith(
       //缓存是否匹配 
       caches.match(event.request).then(function (response) {
         if (response != null) {
           //命中缓存返回缓存，结束请求
           //event.request.method
           //"GET"
           console.log(event.request.url);
           return response
         }
         if (1 == 2) {
           return fetch(event.request.url)
         } else {
           return fetch(event.request).then(function (response) {
             if (!response || response.status !== 200) {
               return response;
             }
             //cache.put(event.request, response.clone());
             if (event.request.method == "GET" && event.request.url.indexOf('/sw.js') == -1) {
               var cloneval = response.clone();
               caches.open(VERSION).then(function (cache) {
                 cache.put(event.request, cloneval);
               })
             }
             return response;
           }).catch(function (e) {
             console.log(e);
             var url = "{dm}/extendfile/ms-element-ui/js/error-404.json";
             return caches.match(url.replace('{dm}', domain))
           });
         }
       })

     )
   });

   self.addEventListener('activate', function (event) {
     event.waitUntil(
       //遍历当前缓存keys
       caches.keys().then(function (cacheNames) {
         var off = true;
         cacheNames.map(function (cacheName) {
           //是否等于当前key,保留自己
           if (cacheName !== VERSION) {
             caches.delete(cacheName);
           } else {
             off = false

           }
         })
         if (off) {
           caches.open(VERSION).then(function (cache) {
             return cache.addAll(cache_list)
             /*.then(
             self.skipWaiting()
             )*/
           })
         }
       }).then(function () {
         self.clients.claim()
       })
     )
   })


 }
 Pwa_Install('{name}_v{version}');